package com.yesep.message.channel;

import com.yesep.constant.Constants;
import com.yesep.downstream.DownStreamDataThread;
import com.yesep.handler.MessageHandler;
import com.yesep.message.MessageBaseService;
import com.yesep.message.MessageInterface;
import com.yesep.model.DataText;
import com.yesep.model.MessageBaseModel;
import com.yesep.utils.DataManager;
import com.yesep.utils.DateUtils;
import io.netty.channel.Channel;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * @program: envm
 * @description: 测试实现类
 * @author: quliang
 * @create: 2019-07-18 14:24
 **/
@Service
public class Message2011Service extends MessageBaseService implements MessageInterface {

    private final static String SUCCESS_CN = "9014";

    private final static String FAILED_CN = "1012";


    /**
     * 对应命令的对应实现
     *
     * @param channel
     * @param messageBaseModel 通讯包实体
     * @param dataText         报文实体
     */
    @Override
    public void handleMessage(Channel channel, MessageBaseModel messageBaseModel, DataText dataText) {
        System.out.println("MessageHandler ...");
        messageBaseModel.setCN(SUCCESS_CN);

        String message = MessageHandler.tranMessageModelToString(messageBaseModel, Constants.CMD_MESSAGE_TEMPLATE_MAP.get(SUCCESS_CN));

        try {
            if (messageBaseModel.getCP().containsKey("DataTime")) {
                String dataTime = (String) messageBaseModel.getCP().get("DataTime");
                if (!Objects.equals(dataTime, null)) {
                    Date nowSystemDate = new Date();

                    Date reqDate = DateUtils.stringToDate(dataTime, DateUtils.DATA_TIME_NO_SPLASH_FORMAT);

                    long overSeconds = DateUtils.secondsAfter(nowSystemDate, reqDate);

                    // 相差在正负60秒钟内则下发相关的指令
                    if (overSeconds > 60L || overSeconds < -60) {
                        String dtType = Constants.DATA_TEXT_STATUS_SUCCESS;



                        MessageBaseModel responseMessages = MessageHandler.tranStringMessageBaseModel(message);
                        responseMessages.setChannel(channel);
                        responseMessages.setIp(channel.remoteAddress().toString());

                        StringBuffer desc = new StringBuffer();
                        try {
                            channel.writeAndFlush(message);
                            //  DownStreamDataThread.putData(responseMessages);
                        } catch (Exception ex) {

                            desc.append("站点异常[").append(messageBaseModel.getMN()).append(",").append(ex.getMessage());
                        }

                        dataText.setDtType(Constants.DATA_TEXT_TYPE_DOWN);
                        dataText.setDtDesc(desc.toString());
                        dataText.setDtStatus(dtType);
                        dataText.setDtContent(message);
                        dataText.setDtCreateTime(new Date());
                        dataText.setReplyId(dataText.getDtId());
                        //保存下行报文
                        DataManager.getDataTextService().save(dataText);
                    }

                }
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
}
